<?php

/**
 * 夏日葵电商系统
 * http://www.xiarikui.com
 */

namespace Admin\Controller;

use Think\AjaxPage;

class CommentController extends BaseController {

    /**
     * 评论
     */
    public function index() {
        $this->display();
    }

    /**
     * 评论内容
     */
    public function detail() {
        $id = I('get.id');
        $rs = M('comment')->where(array('comment_id' => $id))->find();
        if (!$rs) {
            exit($this->error('不存在该评论'));
        }
        $users = M('users')->where(array('user_id' => $rs['user_id']))->find();
        if (IS_POST) {
            $data['parent_id'] = $id;
            $data['content'] = I('post.content');
            $data['goods_id'] = $rs['goods_id'];
            $data['add_time'] = time();
            $data['username'] = 'admin';
            $data['is_show'] = 1;
            $status = M('comment')->add($data);
            if ($status) {
                $this->success('添加成功');
            } else {
                $this->error('添加失败');
            }
            exit;
        }
        $reply = M('comment')->where(array('parent_id' => $id))->select(); // 评论回复列表
        $this->assign('comment', $rs);
        $this->assign('user', $users);
        $this->assign('reply', $reply);
        $this->display();
    }

    /**
     * 删除评论
     */
    public function del() {
        $id = I('get.id');
        $status = M('comment')->where(array('comment_id' => $id))->delete();
        if ($status) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    /**
     * 评论操作
     */
    public function op() {
        $type = I('post.type');
        $selectedId = I('post.selected');
        if (!in_array($type, array('del', 'show', 'hide')) || !$selectedId)
            $this->error('非法操作');
        $where = "comment_id IN ({$selectedId})";
        if ($type == 'del') {
            $where .= " OR parent_id IN ({$selectedId})";
            $status = M('comment')->where($where)->delete();
        }
        if ($type == 'show') {
            $status = M('comment')->where($where)->save(array('is_show' => 1));
        }
        if ($type == 'hide') {
            $status = M('comment')->where($where)->save(array('is_show' => 0));
        }
        if (!$status)
            $this->error('操作失败');
        $this->success('操作成功');
    }

    /**
     * 获取评论数据
     */
    public function ajaxIndex() {
        $commentMod = M('comment');
        $nickname = I('nickname', '', 'trim');
        $content = I('content', '', 'trim');
        $where = ' parent_id = 0';
        if ($nickname) {
            $where .= " AND username='$nickname'";
        }
        if ($content) {
            $where .= " AND content like '%{$content}%'";
        }
        $count = $commentMod->where($where)->count();
        $Page = new AjaxPage($count, 16);
        $show = $Page->show();

        $commentRs = $commentMod->where($where)->order('add_time DESC')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $goodsList = array();
        if (!empty($commentRs)) {
            $goodsIdArr = get_arr_column($commentRs, 'goods_id');
            $goodsList = M('Goods')->where("goods_id in (" . implode(',', $goodsIdArr) . ")")->getField("goods_id,goods_name");
        }
        $this->assign('goodsList', $goodsList);
        $this->assign('comment_list', $commentRs);
        $this->assign('page', $show);
        $this->display();
    }

    /**
     * 咨询
     */
    public function askList() {
        $this->display();
    }

    /**
     * 获取咨询数据
     */
    public function ajaxAskList() {
        $model = M('goods_consult');
        $nickname = I('nickname', '', 'trim');
        $content = I('content', '', 'trim');
        $where = ' parent_id = 0';
        if ($nickname) {
            $where .= " AND username='$nickname'";
        }
        if ($content) {
            $where .= " AND content like '%{$content}%'";
        }
        $count = $model->where($where)->count();
        $Page = new AjaxPage($count, 16);
        $show = $Page->show();

        $commentRs = $model->where($where)->order('add_time DESC')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        if (!empty($commentRs)) {
            $goodsIdArr = get_arr_column($commentRs, 'goods_id');
            $goodsList = M('Goods')->where("goods_id in (" . implode(',', $goodsIdArr) . ")")->getField("goods_id,goods_name");
        }
        $consult_type = array(0 => '默认咨询', 1 => '商品咨询', 2 => '支付咨询', 3 => '配送', 4 => '售后');
        $this->assign('consult_type', $consult_type);
        $this->assign('goodsList', $goodsList);
        $this->assign('comment_list', $commentRs);
        $this->assign('page', $show);
        $this->display();
    }

    /**
     * 咨询
     */
    public function consultInfo() {
        $id = I('get.id');
        $goodsConsultMod = M('goods_consult');
        $rs = $goodsConsultMod->where(array('id' => $id))->find();
        if (!$rs) {
            exit($this->error('不存在该咨询'));
        }
        if (IS_POST) {
            $data['parent_id'] = $id;
            $data['content'] = I('post.content');
            $data['goods_id'] = $rs['goods_id'];
            $data['consult_type'] = $rs['consult_type'];
            $data['add_time'] = time();
            $data['is_show'] = 1;
            $status = $goodsConsultMod->add($data);
            if ($status) {
                $this->success('添加成功');
            } else {
                $this->error('添加失败');
            }
            exit;
        }
        $reply = $goodsConsultMod->where(array('parent_id' => $id))->select(); // 咨询回复列表   	 
        $this->assign('comment', $rs);
        $this->assign('reply', $reply);
        $this->display();
    }

    /**
     * 资讯操作
     */
    public function askHandle() {
        $type = I('post.type');
        $selectedId = I('post.selected');
        if (!in_array($type, array('del', 'show', 'hide')) || !$selectedId)
            $this->error('操作完成');

        $selectedId = implode(',', $selectedId);
        $goodsConsultMod = M('goods_consult');
        if ($type == 'del') {
            $where .= "( id IN ({$selectedId}) OR parent_id IN ({$selectedId})) ";
            $status = $goodsConsultMod->where($where)->delete();
        }
        if ($type == 'show') {
            $status = $goodsConsultMod->where("id IN ({$selectedId})")->save(array('is_show' => 1));
        }
        if ($type == 'hide') {
            $status = $goodsConsultMod->where("id IN ({$selectedId})")->save(array('is_show' => 0));
        }
        $this->success('操作完成');
    }

}
